package com.xyht.sca_s.student_manage_system.modules.workApplicationGroup.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.workApplication.entity.vo.SmsWorkApplicationDetailTorsionVO;
import com.xyht.sca_s.student_manage_system.modules.workApplicationGroup.entity.vo.*;
import com.xyht.sca_s.student_manage_system.modules.workApplicationGroup.service.SmsWorkGroupApplicationService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.Future;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2023-06-12
 */
@SecretBody
@RestController
@RequestMapping("/workApplicationGroup")
public class SmsWorkGroupApplicationController {
    @Autowired
    SmsWorkGroupApplicationService smsWorkGroupApplicationService;
    @Autowired
    JwtTokenUtil jwtTokenUtil;

    @ApiModelProperty(value = "新增工作请示单")
    @PostMapping("/add")
    public ResponseResult smsWorkGroupApplicationAdd(@RequestBody SmsWorkGroupApplicationVO smsWorkGroupApplicationVO,
                                                HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsWorkGroupApplicationService.smsWorkGroupApplicationAdd(smsWorkGroupApplicationVO,user_id);
    }

    @ApiModelProperty(value = "修改工作请示单")
    @PostMapping("/update")
    public ResponseResult smsWorkGroupApplicationUpdate(@RequestBody SmsWorkGroupApplicationVO smsWorkGroupApplicationVO,
                                                   HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsWorkGroupApplicationService.smsWorkGroupApplicationUpdate(smsWorkGroupApplicationVO,user_id);
    }

    @ApiModelProperty(value = "撤销工作请示单")
    @PostMapping("/cancel")
    public ResponseResult smsWorkGroupApplicationCancel(@RequestBody SmsWorkGroupApplicationCancelVO smsWorkGroupApplicationCancelVO,
                                                   HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsWorkGroupApplicationService.smsWorkGroupApplicationCancel(smsWorkGroupApplicationCancelVO,user_id);
    }

    @ApiModelProperty(value = "审批")
    @PostMapping("/allow")
    public ResponseResult allowSmsWorkGroupApplication(@RequestBody SmsWorkGroupApplicationAllowVO allowVO, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsWorkGroupApplicationService.allowSmsWorkApplication(allowVO, user_id);
    }

    @ApiModelProperty(value = "审批列表")
    @GetMapping("/list")
    public ResponseResult getSmsWorkGroupApplicationList(
                                                    @RequestParam(value = "search_str",required = false) String search_str,
                                                    @RequestParam(value = "allowStatus",required = false) Integer allowStatus,
                                                    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                    @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                    @RequestParam(value = "application_type") Integer application_type,
                                                    @RequestParam(value = "sort",required = false) Integer sort,
                                                    HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsWorkGroupApplicationService.getSmsWorkGroupApplicationList(search_str,allowStatus,user_id,application_type,sort,pageNum,pageSize);
    }

    @ApiModelProperty(value = "审批详情")
    @GetMapping("/detail")
    public ResponseResult getSmsWorkGroupApplicationDetail(@RequestParam(value = "application_id") String application_id,
                                                      HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsWorkGroupApplicationService.getSmsWorkGroupApplicationDetail(application_id,user_id);
    }

    @ApiModelProperty(value = "申请人指定流程（申请前获取）")
    @GetMapping("/processList")
    public ResponseResult getSmsWorkGroupApplicationOriginSettingProcess() {
        return smsWorkGroupApplicationService.getSmsWorkGroupApplicationOriginSettingProcess();
    }

    @ApiModelProperty(value = "查询经办人")
    @GetMapping("/select")
    public ResponseResult selectAgent(@RequestParam(value = "person_name", required = false) String person_name){
        return smsWorkGroupApplicationService.selectAgent(person_name);
    }

    @ApiModelProperty(value = "申请表下载")
    @GetMapping("/download")
    public ResponseResult tableDownload(@RequestParam(value = "apply_id") String apply_id,HttpServletResponse response) {
        ResponseResult responseResult;
        try{
            Future<ResponseResult> future = smsWorkGroupApplicationService.tableDownload(apply_id,response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }
    @ApiModelProperty(value = "查询所有申请记录")
    @GetMapping("/selectAll")
    public ResponseResult selectAllApply(
            @RequestParam(value = "apply_unit",required = false) String apply_unit,
            @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "status",required = false) Integer status
    ){
        return smsWorkGroupApplicationService.selectAllApply(apply_unit,
                pageSize,pageNum,status);
    }

    @ApiOperation("批量导出")
    @PostMapping("/batch/export")
    public ResponseResult exportWorkGroupApplication(@RequestBody SmsWorkGroupApplicationExportVO smsWorkGroupApplicationExportVO,
                                            HttpServletResponse response){
        ResponseResult responseResult;
        try{
            Future<ResponseResult> future = smsWorkGroupApplicationService.exportWorkGroupApplication(smsWorkGroupApplicationExportVO, response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }

    @ApiModelProperty(value = "强制归档")
    @PostMapping("/forced")
    public ResponseResult forcedFinishWorkGroupApplication(@RequestBody SmsWorkGroupApplicationForceVO forceVO,HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWorkGroupApplicationService.forcedFinishWorkGroupApplication(forceVO.getApplyId(),userId);
    }

    @ApiModelProperty(value = "强制流转")
    @PostMapping("/forcedTorsion")
    public ResponseResult forcedTorsionWorkGroupApplication(@RequestBody SmsWorkApplicationDetailTorsionVO torsionVO){
        return smsWorkGroupApplicationService.forcedTorsionWorkGroupApplication(torsionVO);
    }

    @ApiModelProperty(value = "代审批")
    @PostMapping("/replaceApply")
    public ResponseResult replaceApplyWorkGroupApplication(HttpServletRequest request,
                                                           @RequestBody SmsWorkGroupApplicationAllowVO allowVO){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWorkGroupApplicationService.replaceApplyWorkGroupApplication(userId,allowVO);
    }


    @ApiModelProperty(value = "强制归档")
    @PostMapping("/forcedReject")
    public ResponseResult forcedRejectWorkGroupApplication(@RequestBody SmsWorkGroupApplicationForceVO forceVO,HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWorkGroupApplicationService.forcedRejectWorkGroupApplication(forceVO,userId);
    }

    @ApiModelProperty(value = "删除集团工作请示单")
    @PostMapping("/deleted")
    public ResponseResult deletedWorkGroupApplication(@RequestParam("applyId") String applyId){
        return smsWorkGroupApplicationService.deletedWorkGroupApplication(applyId);
    }

    @ApiOperation(value = "修改审批意见")
    @PostMapping("/updateOpinion")
    public ResponseResult updateOpinion(@RequestBody SmsWorkGroupApplicationUpdateOpinionVo updateOpinionVo,HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWorkGroupApplicationService.updateOpinion(updateOpinionVo,userId);
    }

    @ApiOperation(value = "修改附件")
    @PostMapping("/updateAnnex")
    public ResponseResult updateAnnex(@RequestBody SmsWorkGroupApplicationUpdateAnnexVo updateAnnexVo,HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWorkGroupApplicationService.updateAnnex(updateAnnexVo,userId);
    }
}

